package by.bsuir.web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import by.bsuir.entity.Node;
import by.bsuir.service.NodeService;
import by.bsuir.service.NodeServicePostgres;

/**
 * Servlet implementation class LoadController
 */
public class LoadController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        int firstIp = 0;
        int lastIp = 254;
        String host = (String) request.getSession().getAttribute("host");
        String login = (String) request.getSession().getAttribute("login");
        String passwd = (String) request.getSession().getAttribute("passwd");
        String domain = (String) request.getSession().getAttribute("domain");
        String search = (String) request.getSession().getAttribute("search");
        Integer sfirstIp = (Integer) request.getSession().getAttribute("firstIp");
        Integer slastIp = (Integer) request.getSession().getAttribute("lastIp");

        if (sfirstIp != null) {
            firstIp = sfirstIp;
        }
        if (slastIp != null) {
            lastIp = slastIp;
        }
        NodeService nodeService = new NodeServicePostgres();
        Node node = nodeService.read(1L);
        if (node == null) {
            node = new Node();
        }
        request.getSession().setAttribute("host", host);
        request.getSession().setAttribute("login", login);
        request.getSession().setAttribute("passwd", passwd);
        request.getSession().setAttribute("domain", domain);
        request.getSession().setAttribute("search", search);
        request.getSession().setAttribute("firstIp", firstIp);
        request.getSession().setAttribute("lastIp", lastIp);
        request.getSession().setAttribute("node", node);
        request.getSession().setAttribute("rootNode", node);

        RequestDispatcher rd = request.getRequestDispatcher("/jsp/scan.jsp");
        rd.forward(request, response);
    }

}
